// pages/result/result.js
Page({
    data: {
        username: '',
        isChecked: false, // 是否有选中的选项
        id: 1, // 选中的选项
        index: -1, // 上一次选中的下表
        ids: [],
        vote: {}, // 投票对象
        resultsLength: 0, // 总票数
        isShow: false, // 是否显示票数以及百分比
        info: false, // 是否显示详情
        infoText: '显示详情',
        isVote: true, // 是否可以投票
        url: 'http://192.168.31.34:8081'
        // url:'http://172.20.10.4:8081'
    },
    // 显示百分比
    showPercent() {
        this.setData({ isShow: true })
    },
    // 选中选项
    check(e) {
        const id = e.currentTarget.dataset.id
        const index = e.currentTarget.dataset.index
        let vote = this.data.vote
        if (!this.data.vote.multiple) { // 如果是单选，只能选择一个
            if (this.data.index != -1) { // 已经有选中的
                vote.ops[this.data.index].checked = false;
            }
        }
        vote.ops[index].checked = true;
        this.setData({ vote, index })
        let checkIds = this.data.ids;
        if (checkIds.indexOf(id) != -1) {
            checkIds.splice(checkIds.indexOf(id), 1)
            vote.ops[index].checked = false;
        } else {
            if (!this.data.vote.multiple) { // 如果是单选 从结果集中删除原有的id
                checkIds.pop()
            }
            checkIds.push(id)
        }
        this.setData({ ids: checkIds, vote })
        if (this.data.ids.length == 0) {
            this.setData({ index: -1 })
        }
    },
    // 编辑
    edit() {
        const _this = this
        wx.redirectTo({
            url: '/pages/vote/vote?vote=' + JSON.stringify(_this.data.vote),
        })
    },
    handleDelete() {
        const _this = this
        wx.request({
            url: _this.data.url + '/vote/vote/remove?ids=' + _this.data.vote.id,
            method: 'POST',
            success() {
                wx.redirectTo({
                    url: '/pages/main/main',
                    success() {
                        wx.showToast({
                            title: '删除成功',
                            icon: 'success',
                        })
                    }
                })
            }
        })
    },
    // 解析后台请求到的数据
    analyse() {
        let results = this.data.vote.results;
        let ids = []
        let ops = []
        for (let i = 0; i < results.length; i++) {
            let opIds = []
            results[i].option.forEach(op => opIds.push(op.optionId))
            let users = []
            opIds.forEach(e => {
                if (ids.indexOf(e) == -1) {
                    ids.push(e)
                    users.push(results[i].username)
                    ops.push({ id: e, optionLength: 1, users: users })
                } else {
                    ops[ids.indexOf(e)].optionLength++
                    ops[ids.indexOf(e)].users.push(results[i].username)
                }
            })
        }
        let resultsLength = 0;
        ops.forEach(e => resultsLength += e.optionLength)
        this.setData({ resultsLength })
        let vote = this.data.vote
        vote.ops.forEach(e => {
            e.len = 0;
            e.users = [];
            e.percent = 0
        })
        for (let i = 0; i < ops.length; i++) {
            for (let j = 0; j < vote.ops.length; j++) {
                if (vote.ops[j].id == ops[i].id) {
                    vote.ops[j].len = ops[i].optionLength
                    vote.ops[j].users = ops[i].users
                    vote.ops[j].percent = Math.floor((parseInt(ops[i].optionLength) / parseInt(this.data.resultsLength)) * 100)
                }
            }
        }
        this.setData({ vote })
        const _ops = this.data.vote.ops
        for (let i = 0; i < _ops.length; i++) {
            if (_ops[i].users.indexOf(this.data.username) != -1) {
                this.setData({ isVote: false })
                break;
            } else {
                this.setData({ isVote: true })
            }
        }
    },
    vote() {
        // 提交投票
        const _this = this;
        wx.request({
            url: _this.data.url + '/vote/result/add1',
            method: "POST",
            data: {
                username: _this.data.username,
                voteId: _this.data.vote.id,
            },
            success: function (res) {
                const id = res.data.data;
                _this.data.ids.forEach(e => wx.request({
                    url: _this.data.url + '/vote/resultOptions/add1',
                    method: 'POST',
                    data: {
                        resultId: id,
                        optionId: e
                    },
                    success: function (res) {
                        wx.redirectTo({
                            url: '/pages/result/result?id=' + _this.data.vote.id,
                        })
                    }
                }))
            }
        })

    },
    showInfo() {
        this.setData({
            info: !this.data.info,
            // infoText: this.data.info ? '隐藏详情' : '显示详情'
        })
        this.setData({
            infoText: this.data.info ? '隐藏详情' : '显示详情'
        })
    },
    onLoad(options) {
        if (options) {
            this.setData({ id: options.id })
        }
        const _this = this;
        wx.request({
            url: _this.data.url + '/vote/vote/lists/' + _this.data.id,
            success: function (res) {
                _this.setData({
                    vote: res.data
                })
                _this.analyse()
            }
        })
        wx.getStorage({
            key: 'username',
            success: function (res) {
                _this.setData({ username: res.data })
            }
        })
    },

})